package cn.sise.edu.crm.service.impl;

import cn.sise.edu.crm.domain.Role;
import cn.sise.edu.crm.mapper.RoleMapper;
import cn.sise.edu.crm.query.QueryObject;
import cn.sise.edu.crm.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by chen on 2019/11/3.
 */
@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void insert(Role role, Long[] ids) {
        roleMapper.insert(role);
        if(ids != null){
            for (Long id : ids) {
                roleMapper.insertRelation(role.getId(),id);
            }
        }
    }

    @Override
    public void delete(Long id) {
        roleMapper.deleteByRoleId(id);
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Role role, Long[] ids) {
        roleMapper.updateByPrimaryKey(role);
        roleMapper.deleteByRoleId(role.getId());
        if(ids !=null){
            for (Long id : ids) {
                roleMapper.insertRelation(role.getId(),id);
            }
        }

    }

    @Override
    public Role selectById(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }

    @Override
    public PageInfo<Role> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo<>(roleMapper.selectForList(qo));
    }

    @Override
    public List<String> selectByEmployee(Long id) {
        return roleMapper.selectByEmployee(id);
    }
}
